/**
 * Created by apple on 16/9/29.
 */
var time_text = "";
var byDay = "";
var byWeek = "";
var byMonth = "";

$(document).ready(function () {
    time_text = "24小时内";
    byDay = 1;
    byWeek = "";
    byMonth = "";

    //排行榜列表
    rank_list();
    //空榜单
    rank_empty();

    //导航栏选择时间
    $(".rank-nav-box a").click(function () {
        $(".rank-nav-box a").removeClass('current');
        $(this).addClass('current');
        time_text = $(this).html();
        $('.rank-all').html('');
        if (time_text == "24小时内") {
            byDay = 1;
            byWeek = "";
            byMonth = "";
        } else if (time_text == "最近7天内") {
            byDay = "";
            byWeek = 1;
            byMonth = "";
        } else if (time_text == "一个月内") {
            byDay = "";
            byWeek = "";
            byMonth = 1;
        } else {
            byDay = "";
            byWeek = "";
            byMonth = "";
        }
        //排行榜列表
        rank_list();
        //空榜单
        rank_empty();
    });
});

//排行榜列表
function rank_list() {
    //文章被赏最多排行榜
    articleRank();
    //热门帖子排行榜
    seeRank();
    //点赞最多排行榜
    likeRank();
    //评论最多排行榜
    commentRank();
    //最爱聊天排行榜
    chatRank();
    //送礼最多排行榜
    userRank();
    //粉丝增长排行榜
    fansRank();
}


//空榜单
function rank_empty() {
    var rank_empty = "";
    var _num = ($('.rank-all').children('.rank-box').length) % 2;
    if (_num == 0) {
        //列表为左侧内容
        rank_empty += "<div class='rank-box rank-empty'>";
    } else {
        //列表为右侧内容
        rank_empty += "<div class='rank-box rank-empty' style='margin-left: 20px;'>";
    }
    rank_empty += "<span>更多榜单<br/>敬请期待</span></div>"
    if (_num != 0) {
        rank_empty += "<div style='clear: both'></div>"
    }
    if ($('.rank-all').children().length == 0) {
        $('.rank-all').html(rank_empty)
    } else {
        $('.rank-all').append(rank_empty)
    }
}


//文章被赏最多排行榜
function articleRank() {
    $.ajax({
        type: 'get',
        url: url_link + 'gifts/articleRank',
        data: {
            byDay: byDay,
            byWeek: byWeek,
            byMonth: byMonth
        },
        async: false,
        success: function (data) {
            try {
                var obj = JSON.parse(data);
                var chatRank_info = "";
                if (obj.code == 0) {
                    var _num = ($('.rank-all').children('.rank-box').length) % 2;
                    var m = 0;
                    if (_num == 0) {
                        //列表为左侧内容
                        chatRank_info += "<div class='rank-box'>";
                    } else {
                        //列表为右侧内容
                        chatRank_info += "<div class='rank-box' style='margin-left: 20px;'>";
                    }
                    chatRank_info += "<h3>" + time_text + "文章被赏最多排行榜</h3><ul class='thread-rank'>";
                    if (obj.data.length == 0) {
                        chatRank_info += "<div class='none_rank'>暂无排名</div>"
                    } else {
                        $.each(obj.data, function (i, item) {
                            m = i + 1;
                            if (i < 3) {
                                if (i == 0) {
                                    chatRank_info += "<li><dl class='first'>"
                                } else if (i == 1) {
                                    chatRank_info += "<li><dl class='second'>"
                                } else {
                                    chatRank_info += "<li style='margin-left: 0;'><dl class='third' style='padding-left: 20px;'>"
                                }
                                chatRank_info += "<dt>" + m + "</dt>" +
                                    "<dd>" +
                                    "<a href='publish/show.html?article_id=" + item.article_id + "' target='_blank' class='img-box'>";
                                if (item.cover) {
                                    chatRank_info += "<img src='" + item.cover + "' style='width: 130px;height: 86px;'>";
                                } else {
                                    var _content = item.articleVo.content.split("$$##MotorTravel##$$");
                                    var _count = 0;
                                    for (var _a = 0; _a < _content.length; _a++) {
                                        var m = _content[_a].substring(0, 13);
                                        if (m == "$$##Image##$$") {
                                            //有图
                                            _count = 1;
                                            var _content_image_description = _content[_a].split("$$##ImageDescription##$$");
                                            var _content_image_url = _content_image_description[0].split("$$##ImageInfo##$$");
                                            chatRank_info += "<img src='" + _content_image_url[0].substring(13, _content_image_url[0].length) + "' style='width: 130px;height: 86px;'>"
                                            break;
                                        }
                                    }
                                    //纯文字,没有图的情况下,使用备用图片
                                    if (_count == 0) {
                                        chatRank_info += "<img src='../image/base/none_img.png' style='width: 130px;height: 86px;'/>"
                                    }
                                }


                                chatRank_info += "</a>" +
                                    "<div class='content' > " +
                                    "<h4>" +
                                    "<a href='publish/show.html?article_id=" + item.article_id + "' target='_blank'>" + item.articleVo.title + "</a>" +
                                    "</h4>" +
                                    "<div class='user-info'>" +
                                    "<a href='lookat/article.html?user_id=" + item.articleVo.user.user_id + "' target='_blank' class='headpic'>";
                                if (item.articleVo.user.avatar) {
                                    chatRank_info += "<i><img src=" + item.articleVo.user.avatar + " style='width: 25px;height: 25px;'></i>";
                                } else {
                                    chatRank_info += "<i><img src='../image/base/none.png' style='width: 25px;height: 25px;'></i>";
                                }
                                chatRank_info += "<span>" + item.articleVo.user.nickname + "</span>" +
                                    "</a>" +
                                    "<span class='click-count'><i></i>" + item.articleVo.see_count + "</span>" +
                                    "</div></div></dd></dl></li>"
                            } else if (i < 10) {
                                chatRank_info += "<li><dl class='others'>" +
                                    "<dt>" + m + "</dt>" +
                                    "<dd>" +
                                    "<h4><a href='publish/show.html?article_id=" + item.article_id + "' target='_blank'>" + item.articleVo.title + "</a></h4>" +
                                    "<span class='click-count'><i></i>" + item.articleVo.see_count + "</span>" +
                                    "</dd></dl></li>"
                            }
                        });
                    }

                    chatRank_info += "</ul></div>";
                    if (_num != 0) {
                        chatRank_info += "<div style='clear: both'></div>"
                    }
                    if ($('.rank-all').children().length == 0) {
                        $('.rank-all').html(chatRank_info)
                    } else {
                        $('.rank-all').append(chatRank_info)
                    }
                }
            } catch (e) {

            }
        }
    })

}


//热门帖子排行榜
function seeRank() {
    article_id = 0;
    mostSee = 1;
    mostLike = '';
    mostComment = '';
    article_sort()
}


//点赞最多排行榜
function likeRank() {
    article_id = 0;
    mostSee = '';
    mostLike = 1;
    mostComment = '';
    article_sort()
}


//评论最多排行榜
function commentRank() {
    article_id = 0;
    mostSee = '';
    mostLike = '';
    mostComment = 1;
    article_sort()
}


//最爱聊天排行
function chatRank() {
    $.ajax({
        type: 'get',
        url: url_link + 'comments/chatRank',
        data: {
            byDay: byDay,
            byWeek: byWeek,
            byMonth: byMonth
        },
        async: false,
        success: function (data) {
            try {
                var obj = JSON.parse(data);
                var chatRank_info = "";
                if (obj.code == 0) {
                    var _num = ($('.rank-all').children('.rank-box').length) % 2;
                    var m = 0;
                    if (_num == 0) {
                        //列表为左侧内容
                        chatRank_info += "<div class='rank-box'>";
                    } else {
                        //列表为右侧内容
                        chatRank_info += "<div class='rank-box' style='margin-left: 20px;'>";
                    }
                    chatRank_info += "<h3>" + time_text + "最爱聊天排行榜</h3><ul class='user-rank'>";
                    if (obj.data.length == 0) {
                        chatRank_info += "<div class='none_rank'>暂无排名</div>"
                    } else {
                        $.each(obj.data, function (i, item) {
                            m = i + 1;
                            if (i < 3) {
                                if (i == 0) {
                                    chatRank_info += "<li class='first'>"
                                } else if (i == 1) {
                                    chatRank_info += "<li class='second'>"
                                } else {
                                    chatRank_info += "<li class='third'>"
                                }
                                chatRank_info += "<dl>" +
                                    "<dt>" + m + "</dt>" +
                                    "<dd>" +
                                    "<a href='lookat/article.html?user_id=" + item.user_id + "'  target='_blank' class='headpic'>"
                                if (item.user.avatar) {
                                    chatRank_info += "<i><img src=" + item.user.avatar + " style='width: 120px;height: 120px;'></i>";
                                } else {
                                    chatRank_info += "<i><img src='../image/base/none.png' style='width: 120px;height: 120px;'></i>";
                                }
                                if (item.user.sex == 1) {
                                    //男的
                                    chatRank_info += "<b class='male-mid'></b>"
                                } else {
                                    //女的
                                    chatRank_info += "<b class='female-mid'></b>"
                                }
                                chatRank_info += "</a>" +
                                    "<h4>" +
                                    "<a href='lookat/article.html?user_id=" + item.user_id + "'  target='_blank' class='username'>" + item.user.nickname + "</a>" +
                                    "<span><i class='talk'></i>" + item.comment_count + "</span>" +
                                    "</h4></dd></dl></li>";
                                if (i == 2) {
                                    chatRank_info += "<div style='clear: both'></div>"
                                }
                            } else if (i < 10) {
                                chatRank_info += "<li class='others'>" +
                                    "<dl>" +
                                    "<dt>" + m + "</dt>" +
                                    "<dd>" +
                                    "<a href='lookat/article.html?user_id=" + item.user_id + "' target='_blank' class='headpic'>"
                                if (item.user.avatar) {
                                    chatRank_info += "<i><img src=" + item.user.avatar + " style='width: 25px;height: 25px;'></i>";
                                } else {
                                    chatRank_info += "<i><img src='../image/base/none.png' style='width: 25px;height: 25px;'></i>";
                                }
                                if (item.user.sex == 1) {
                                    //男的
                                    chatRank_info += "<b class='male-small'></b>"
                                } else {
                                    //女的
                                    chatRank_info += "<b class='female-small'></b>"
                                }
                                chatRank_info += "</a>" +
                                    "<h4>" +
                                    "<a href='lookat/article.html?user_id=" + item.user_id + "' target='_blank' class='username'>" + item.user.nickname + "<b class='adminIco'></b>" +
                                    "</a>" +
                                    "<span><i class='talk'></i>" + item.comment_count + "</span>" +
                                    "</h4></dd></dl></li>"
                            }
                        });
                    }
                    chatRank_info += "</ul></div>";
                    if (_num != 0) {
                        chatRank_info += "<div style='clear: both'></div>"
                    }
                    if ($('.rank-all').children().length == 0) {
                        $('.rank-all').html(chatRank_info)
                    } else {
                        $('.rank-all').append(chatRank_info)
                    }
                }
            } catch (e) {

            }
        }
    })

}


//送礼最多排行榜
function userRank() {
    $.ajax({
        type: 'get',
        url: url_link + 'gifts/userRank',
        data: {
            byDay: byDay,
            byWeek: byWeek,
            byMonth: byMonth
        },
        async: false,
        success: function (data) {
            try {
                var obj = JSON.parse(data);
                var chatRank_info = "";
                if (obj.code == 0) {
                    var _num = ($('.rank-all').children('.rank-box').length) % 2;
                    var m = 0;
                    if (_num == 0) {
                        //列表为左侧内容
                        chatRank_info += "<div class='rank-box'>";
                    } else {
                        //列表为右侧内容
                        chatRank_info += "<div class='rank-box' style='margin-left: 20px;'>";
                    }
                    chatRank_info += "<h3>" + time_text + "送礼最多排行榜</h3><ul class='user-rank'>";
                    if (obj.data.length == 0) {
                        chatRank_info += "<div class='none_rank'>暂无排名</div>"
                    } else {
                        $.each(obj.data, function (i, item) {
                            m = i + 1;
                            if (i < 3) {
                                if (i == 0) {
                                    chatRank_info += "<li class='first'>"
                                } else if (i == 1) {
                                    chatRank_info += "<li class='second'>"
                                } else {
                                    chatRank_info += "<li class='third'>"
                                }
                                chatRank_info += "<dl>" +
                                    "<dt>" + m + "</dt>" +
                                    "<dd>" +
                                    "<a href='lookat/article.html?user_id=" + item.user_id + "' target='_blank' class='headpic'>";
                                if (item.user.avatar) {
                                    chatRank_info += "<i><img src=" + item.user.avatar + " style='width: 120px;height: 120px;'></i>";
                                } else {
                                    chatRank_info += "<i><img src='../image/base/none.png' style='width: 25px;height: 25px;'></i>";
                                }
                                if (item.user.sex == 1) {
                                    //男的
                                    chatRank_info += "<b class='male-mid'></b>"
                                } else {
                                    //女的
                                    chatRank_info += "<b class='female-mid'></b>"
                                }
                                chatRank_info += "</a>" +
                                    "<h4>" +
                                    "<a href='lookat/article.html?user_id=" + item.user_id + "' target='_blank' class='username'>" + item.user.nickname + "</a>" +
                                    "<span class='gift-count'><i></i>" + item.money + "</span>" +
                                    "</h4></dd></dl></li>";
                                if (i == 2) {
                                    chatRank_info += "<div style='clear: both'></div>"
                                }
                            } else if (i < 10) {
                                chatRank_info += "<li class='others'>" +
                                    "<dl>" +
                                    "<dt>" + m + "</dt>" +
                                    "<dd>" +
                                    "<a href='lookat/article.html?user_id=" + item.user_id + "' target='_blank' class='headpic'>";
                                if (item.user.avatar) {
                                    chatRank_info += "<i><img src=" + item.user.avatar + " style='width: 25px;height: 25px;'></i>";
                                } else {
                                    chatRank_info += "<i><img src='../image/base/none.png' style='width: 25px;height: 25px;'></i>";
                                }
                                if (item.user.sex == 1) {
                                    //男的
                                    chatRank_info += "<b class='male-small'></b>"
                                } else {
                                    //女的
                                    chatRank_info += "<b class='female-small'></b>"
                                }
                                chatRank_info += "</a>" +
                                    "<h4>" +
                                    "<a href='lookat/article.html?user_id=" + item.user_id + "' target='_blank' class='username'>" + item.user.nickname + "<b class='adminIco'></b>" +
                                    "</a>" +
                                    "<span class='gift-count'><i></i>" + item.money + "</span>" +
                                    "</h4></dd></dl></li>"
                            }
                        });
                    }

                    chatRank_info += "</ul></div>";
                    if (_num != 0) {
                        chatRank_info += "<div style='clear: both'></div>"
                    }
                    if ($('.rank-all').children().length == 0) {
                        $('.rank-all').html(chatRank_info)
                    } else {
                        $('.rank-all').append(chatRank_info)
                    }
                }
            } catch (e) {

            }
        }
    })

}


//粉丝增长排行榜
function fansRank() {
    $.ajax({
        type: 'get',
        url: url_link + 'follows/fansRank',
        data: {
            byDay: byDay,
            byWeek: byWeek,
            byMonth: byMonth
        },
        async: false,
        success: function (data) {
            try {
                var obj = JSON.parse(data);
                var chatRank_info = "";
                if (obj.code == 0) {
                    var _num = ($('.rank-all').children('.rank-box').length) % 2;
                    var m = 0;
                    if (_num == 0) {
                        //列表为左侧内容
                        chatRank_info += "<div class='rank-box'>";
                    } else {
                        //列表为右侧内容
                        chatRank_info += "<div class='rank-box' style='margin-left: 20px;'>";
                    }
                    chatRank_info += "<h3>" + time_text + "粉丝增长排行榜</h3><ul class='user-rank'>";
                    if (obj.data.length == 0) {
                        chatRank_info += "<div class='none_rank'>暂无排名</div>"
                    } else {
                        $.each(obj.data, function (i, item) {
                            m = i + 1;
                            if (i < 3) {
                                if (i == 0) {
                                    chatRank_info += "<li class='first'>"
                                } else if (i == 1) {
                                    chatRank_info += "<li class='second'>"
                                } else {
                                    chatRank_info += "<li class='third'>"
                                }
                                chatRank_info += "<dl>" +
                                    "<dt>" + m + "</dt>" +
                                    "<dd>" +
                                    "<a href='lookat/article.html?user_id=" + item.user.user_id + "' target='_blank' class='headpic'>";
                                if (item.user.avatar) {
                                    chatRank_info += "<i><img src=" + item.user.avatar + " style='width: 120px;height: 120px;'></i>";
                                } else {
                                    chatRank_info += "<i><img src='../image/base/none.png' style='width: 120px;height: 120px;'></i>";
                                }
                                if (item.user.sex == 1) {
                                    //男的
                                    chatRank_info += "<b class='male-mid'></b>"
                                } else {
                                    //女的
                                    chatRank_info += "<b class='female-mid'></b>"
                                }
                                chatRank_info += "</a>" +
                                    "<h4>" +
                                    "<a href='lookat/article.html?user_id=" + item.user.user_id + "' target='_blank' class='username'>" + item.user.nickname + "</a>" +
                                    "<span><i class='fans'></i>" + item.fans_count + "</span>" +
                                    "</h4></dd></dl></li>";
                                if (i == 2) {
                                    chatRank_info += "<div style='clear: both'></div>"
                                }
                            } else if (i < 10) {
                                chatRank_info += "<li class='others'>" +
                                    "<dl>" +
                                    "<dt>" + m + "</dt>" +
                                    "<dd>" +
                                    "<a href='lookat/article.html?user_id=" + item.user.user_id + "' target='_blank' class='headpic'>";
                                if (item.user.avatar) {
                                    chatRank_info += "<i><img src=" + item.user.avatar + " style='width: 25px;height: 25px;'></i>";
                                } else {
                                    chatRank_info += "<i><img src='../image/base/none.png' style='width: 25px;height: 25px;'></i>";
                                }
                                if (item.user.sex == 1) {
                                    //男的
                                    chatRank_info += "<b class='male-small'></b>"
                                } else {
                                    //女的
                                    chatRank_info += "<b class='female-small'></b>"
                                }
                                chatRank_info += "</a>" +
                                    "<h4>" +
                                    "<a href='lookat/article.html?user_id=" + item.user.user_id + "' target='_blank' class='username'>" + item.user.nickname + "<b class='adminIco'></b>" +
                                    "</a>" +
                                    "<span><i class='fans'></i>" + item.fans_count + "</span>" +
                                    "</h4></dd></dl></li>"
                            }
                        });
                    }

                    chatRank_info += "</ul></div>";
                    if (_num != 0) {
                        chatRank_info += "<div style='clear: both'></div>"
                    }
                    if ($('.rank-all').children().length == 0) {
                        $('.rank-all').html(chatRank_info)
                    } else {
                        $('.rank-all').append(chatRank_info)
                    }
                }
            } catch (e) {

            }
        }
    })

}


//文章排序
function article_sort() {
    $.ajax({
        type: 'get',
        url: url_link + 'comments/commentRank',
        data: {
            byDay: byDay,
            byWeek: byWeek,
            byMonth: byMonth,
            article_id: article_id,
            mostSee: mostSee,
            mostLike: mostLike,
            mostComment: mostComment
        },
        async: false,
        success: function (data) {
            try {
                var obj = JSON.parse(data);
                var chatRank_info = "";
                if (obj.code == 0) {
                    var _num = ($('.rank-all').children('.rank-box').length) % 2;
                    var m = 0;
                    if (_num == 0) {
                        //列表为左侧内容
                        chatRank_info += "<div class='rank-box'>";
                    } else {
                        //列表为右侧内容
                        chatRank_info += "<div class='rank-box' style='margin-left: 20px;'>";
                    }
                    if (mostSee == 1) {
                        chatRank_info += "<h3>" + time_text + "热门帖子排行榜</h3>"
                    } else if (mostLike == 1) {
                        chatRank_info += "<h3>" + time_text + "点赞最多排行榜</h3>"
                    } else if (mostComment == 1) {
                        chatRank_info += "<h3>" + time_text + "评论最多排行榜</h3>"
                    }
                    chatRank_info += "<ul class='thread-rank'>";
                    if (obj.data.length == 0) {
                        chatRank_info += "<div class='none_rank'>暂无排名</div>"
                    } else {
                        $.each(obj.data, function (i, item) {
                            m = i + 1;
                            if (i < 3) {
                                if (i == 0) {
                                    chatRank_info += "<li><dl class='first'>"
                                } else if (i == 1) {
                                    chatRank_info += "<li><dl class='second'>"
                                } else {
                                    chatRank_info += "<li style='margin-left: 0;'><dl class='third' style='padding-left: 20px;'>"
                                }
                                chatRank_info += "<dt>" + m + "</dt><dd><a href='publish/show.html?article_id=" + item.article_id + "' target='_blank' class='img-box'>";
                                if (item.cover) {
                                    chatRank_info += "<img src='" + item.cover + "' style='width: 130px;height: 86px;'>";
                                } else {
                                    var _content = item.articleVo.content.split("$$##MotorTravel##$$");
                                    var _count = 0;
                                    for (var _a = 0; _a < _content.length; _a++) {
                                        var m = _content[_a].substring(0, 13);
                                        if (m == "$$##Image##$$") {
                                            //有图
                                            _count = 1;
                                            var _content_image_description = _content[_a].split("$$##ImageDescription##$$");
                                            var _content_image_url = _content_image_description[0].split("$$##ImageInfo##$$");
                                            chatRank_info += "<img src='" + _content_image_url[0].substring(13, _content_image_url[0].length) + "' style='width: 130px;height: 86px;'>"
                                            break;
                                        }
                                    }
                                    //纯文字,没有图的情况下,使用备用图片
                                    if (_count == 0) {
                                        chatRank_info += "<img src='../image/base/none_img.png' style='width: 130px;height: 86px;'/>"
                                    }
                                }
                                chatRank_info += "</a>" +
                                    "<div class='content'>" +
                                    "<h4><a href='publish/show.html?article_id=" + item.article_id + "' target='_blank'>" + item.articleVo.title + "</a></h4>" +
                                    "<div class='user-info'>" +
                                    "<a href='lookat/article.html?user_id=" + item.articleVo.user.user_id + "' target='_blank' class='headpic'>"
                                if (item.articleVo.user.avatar) {
                                    chatRank_info += "<i><img src=" + item.articleVo.user.avatar + " style='width: 25px;height: 25px;'></i>";
                                } else {
                                    chatRank_info += "<i><img src='../image/base/none.png' style='width: 25px;height: 25px;'></i>";
                                }
                                chatRank_info += "<span>" + item.articleVo.user.nickname + "</span>" +
                                    "</a>" +
                                    "<span class='click-count'><i></i>" + item.articleVo.see_count + "</span>" +
                                    "</div></div></dd></dl></li>"
                            } else if (i < 10) {
                                chatRank_info += "<li><dl class='others'>" +
                                    "<dt>" + m + "</dt>" +
                                    "<dd>" +
                                    "<h4><a href='publish/show.html?article_id=" + item.article_id + "' target='_blank'>" + item.articleVo.title + "</a></h4>" +
                                    "<span class='click-count'><i></i>" + item.articleVo.see_count + "</span>" +
                                    "</dd></dl></li>"
                            }
                        });
                    }

                    chatRank_info += "</ul></div>";
                    if (_num != 0) {
                        chatRank_info += "<div style='clear: both'></div>"
                    }
                    if ($('.rank-all').children().length == 0) {
                        $('.rank-all').html(chatRank_info)
                    } else {
                        $('.rank-all').append(chatRank_info)
                    }
                }
            } catch (e) {

            }
        }
    })
}